﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.TicketLogic.Dtos;

namespace Yozeev.Controllers.TicketAccount
{
    /// <summary>
    /// 财务收款
    /// </summary>
    [Route("api/TicketAccount/[Controller]/[Action]")]
    
    public class FinanceController: YozeevControllerBase
    {
        private readonly IActivityManager _activityManager;

        private readonly IAccountManager _ticketAccountManager;

        private readonly IRepository<Activity, Guid> _activityRepository;

        private readonly IRepository<Account, Guid> _ticketAccountRepository;

        private readonly IRepository<TicketDetail, Guid> _ticketDetailRepository;

        public FinanceController(
            IActivityManager activityManager,
            IAccountManager ticketAccountManager,
            IRepository<Activity, Guid> activityRepository,
            IRepository<Account, Guid> ticketAccountRepository,
            IRepository<TicketDetail, Guid> ticketDetailRepository)
        {
            _activityManager = activityManager;
            _ticketAccountManager = ticketAccountManager;
            _activityRepository = activityRepository;
            _ticketAccountRepository = ticketAccountRepository;
            _ticketDetailRepository = ticketDetailRepository;
        }

        /// <summary>
        /// 账单明细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<AccountDetailDto>> Detail(string accountId)
        {
            var activities = _activityRepository.GetAll()
                .Where(activity => accountId.Contains(activity.Account.Id.ToString()));
            var activityIds = activities.Select(a => a.Id).ToList();
            var tickets = _ticketDetailRepository.GetAll()
                .Where(t => activityIds.Contains(t.ActivityDetail.ActivityId));

            List<AccountDetailDto> result = new List<AccountDetailDto>();
            foreach (var activity in activities)
            {
                List<TicketDetail> t = tickets.Where(m
                    => m.ActivityDetail.Activity.Id.Equals(activity.Id)).ToList();
                var ticketDtos = t.MapTo<List<TicketDetailListDto>>();
                var accountDetail = activity.MapTo<AccountDetailDto>();
                accountDetail.Tickets = ticketDtos;
                result.Add(accountDetail);
            }

            return result;
        }
    }
}
